package com.igeek.yygh.controller;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.yygh.constant.MessageConstant;
import com.igeek.yygh.model.elders.Elders;
import com.igeek.yygh.result.Results;
import com.igeek.yygh.vo.EldersVo;
import com.igeek.yygh.result.Result;
import com.igeek.yygh.service.EldersService;
import com.igeek.yygh.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author Guzhenghui
 * @since 2024/8/18
 */
@RestController
@RequestMapping("/admin/elders")
@Slf4j
@Api(tags = "老人管理")
public class EldersController {
    @Autowired
    private EldersService eldersService;
    @ApiOperation("分页查询老人信息设置列表")
    @PostMapping("findAllByPage/{current}/{limit}")
    public Result findAll(
            @PathVariable("current")Integer current ,
            @PathVariable("limit")Integer limit ,
            @RequestBody EldersVo eldersVo){
        Page page = Page.of(current, limit);

//        //测试 全局异常处理器 + 自定义异常
//        try {
//            int i = 10/0;
//        }catch (YyghException e){
//            throw new RuntimeException(e.getMessage());
//        }
        LambdaQueryWrapper<Elders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.hasLength(eldersVo.getNumberId()) , Elders::getNumberId , eldersVo.getNumberId());
        lambdaQueryWrapper.like(StringUtils.hasLength(eldersVo.getName()) , Elders::getName , eldersVo.getName());
        Page<Elders> hospitalSetPage =  eldersService.page(page , lambdaQueryWrapper);
        return Result.ok(hospitalSetPage);
    }

    @GetMapping("findPage")
    @ApiOperation("条件分页查询检查项信息")
    public Results findPage(PageQueryVO vo){
        try {
            Page<Elders> page = eldersService.findPage(vo);
            if(page!=null){
                return new Results(true ,MessageConstant.QUERY_CHECKITEM_SUCCESS,page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Results(false , MessageConstant.QUERY_CHECKITEM_FAIL);
    }
    @ApiOperation("根据编号删除老人信息")
    @DeleteMapping("delete/{id}")
    public Result delete(@ApiParam("老人设置编号") @PathVariable("id")Long id){
        boolean flag = eldersService.removeById(id);
        if(flag){
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation("医院设置批量删除")
    @DeleteMapping("/deleteAll")
    public Result deleteAll(@RequestBody Long[] ids){
        if(!ArrayUtil.hasEmpty(ids)){
            boolean flag = eldersService.removeBatchByIds(Arrays.asList(ids));
            if(flag){
                return Result.ok();
            }
        }
        return Result.fail();
    }


    @ApiOperation("获取老人经纬度信息")
    @GetMapping("getEldersLocation/{id}")
    public Map<String, Object> getEldersLocation(@PathVariable("id")Long id){
//        String elders = eldersService.getLocation(id);
//        return Result.ok(elders);
        return (Map<String, Object>) eldersService.getElderById(id);
    }
    @ApiOperation("获取全部老人地理信息")
    @GetMapping("getEldersLocation")
    public List<Map<String, Object> > getEldersLocationAll(){
        return (List<Map<String, Object>>) eldersService.getEldersLocationAll();
    }
}
